/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.tramite.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.cip.esymons.general.be.Area;
import org.cip.esymons.tramite.beans.AreaController;

/**
 *
 * @author FreddyHIQ
 */
public class AreaConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }

        if (value.equals("Seleccione Area") || value.equals("No existen Ambientes")) {
            return null;
        }

        Integer id = new Integer(value);
        AreaController controller = (AreaController) context.getApplication().getELResolver().getValue(context.getELContext(), null, "areaController");
        try {
            return controller.getDelegado().find(id);
        } catch (Exception ex) {
            Logger.getLogger(AreaConverter.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Area) {
            Area o = (Area) value;
            return (o.getAreCodigo() == null ? "" : o.getAreCodigo().toString());
        } else {
            return null;
        }
    }
}
